<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['basicInformation:informationRegister:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basicInformation:informationRegister:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="deploymentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="类型" align="center" prop="type" :formatter="typeFormat"/>
      <el-table-column label="责任管理区" align="center" prop="area"/>
      <el-table-column label="第一责任人姓名" align="center" prop="firstName"/>
      <el-table-column label="具体责任人姓名" align="center" prop="specificName"/>

      <el-table-column label="船管站" align="center" prop="shipOffice"/>
      <el-table-column label="工作站" align="center" prop="workStation"/>
      <el-table-column label="责任区" align="center" prop="zoneArea"/>
      <el-table-column label="巡护人员" align="center" prop="patrolName"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basicInformation:informationRegister:edit']"
          >修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改信息登记对话框 -->
    <el-dialog :title="'选择巡护人员'"
               :visible="open"
               :fullscreen="true"
               class="dialog_con"
               @open="handleOpenDialog"
    >
      <section>
        <el-row>
          <el-col :span="12" class="leftForm">
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
              <el-form-item label="船管所" prop="shipOffice">
                <el-input v-model="form.shipOffice" placeholder="请输入船管所"/>
              </el-form-item>
              <el-form-item label="工作站" prop="workStation">
                <el-input v-model="form.workStation" placeholder="请输入工作站"/>
              </el-form-item>
              <el-form-item label="责任区" prop="zoneArea">
                <el-input v-model="form.zoneArea" placeholder="请输入责任区"/>
              </el-form-item>
              <el-form-item label="坐标" prop="xy">
                <el-input style="width: 90%;" v-model="form.xy" placeholder="请定位坐标" readonly/>
                <el-button style="width: 10%;" @click="point" type="primary" size="medium">定位</el-button>
              </el-form-item>
              <el-form-item label="巡护人员姓名" prop="patrolName">
                <el-input style="width: 90%;" v-model="form.patrolName" placeholder="请输入巡护人员姓名" readonly/>
                <el-button style="width: 10%;" @click="selectPatrol" type="primary" size="medium">选择</el-button>
              </el-form-item>
              <el-form-item label="职务" prop="post" readonly>
                <el-input v-model="form.post" placeholder="请输入职务" readonly/>
              </el-form-item>
              <el-form-item label="所属部门" prop="deptName">
                <el-input v-model="form.deptName" placeholder="请输入所属部门" readonly/>
              </el-form-item>
              <el-form-item label="联系方式" prop="mobile">
                <el-input v-model="form.mobile" placeholder="请输入联系方式" readonly/>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="12" class="rightMap">
            <!-- 地图展示 -->
            <common-map ref="map" class="rightMap"></common-map>
          </el-col>
        </el-row>
      </section>
      <section slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </section>
    </el-dialog>
    <el-dialog :title="titleappUser" :visible.sync="openappUser" width="800px" append-to-body>
      <el-form :model="queryappUserParams" ref="queryappUserForm" :inline="true" v-show="showSearchappUser"
               label-width="68px">
        <el-form-item label="用户账号" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户账号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入姓名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryappUser">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetappUserQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="appUserList" @selection-change="handleSelectionChangeUser">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
        <el-table-column label="用户账号" align="center" prop="userName"/>
        <el-table-column label="姓名" align="center" prop="name"/>
        <el-table-column label="身份证号码" align="center" prop="idcard"/>
        <el-table-column label="联系方式" align="center" prop="phonenumber"/>
        <el-table-column label="职位" align="center" prop="position" :formatter="positionFormat"/>
        <el-table-column label="部门" align="center" prop="dept.deptNmae"/>
      </el-table>
      <pagination
        v-show="totalappUser>0"
        :total="totalappUser"
        :page.sync="queryappUserParams.pageNum"
        :limit.sync="queryappUserParams.pageSize"
        @pagination="getappUserList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okappUser">确 定</el-button>
        <el-button @click="cancelappUser">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listDeployment,
    getDeployment,
    exportDeployment,
    editExt
  } from '@/api/basicInformation/deployment'
  import {listAppUser} from '@/api/basicInformation/appUser'
  import {getUser} from '../../../api/system/user'

  export default {
    name: 'InformationRegister',
    components: {},
    data() {
      return {
        map: null,
        idsappUser: [],
        appUserNames: [],
        thisAppUser: {},
        // 显示搜索条件
        showSearchappUser: true,
        totalappUser: 0,
        appUserList: [],
        titleappUser: '选择勤务信息',
        openappUser: false,
        queryappUserParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          userName: undefined,
          userType: 2
        },
        // 类型字典
        typeOptions: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 信息登记表格数据
        deploymentList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 职位字典
        positionOptions: [],
        sexOptions: [],
        // 查询参数
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          type: undefined,
          orderByColumn: 'id',
          isAsc: 'desc'
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        markers: [],
        url: process.env.BASE_URL
      }
    },
    created() {
      this.getList()
      this.getDicts('service_deployment_type').then(response => {
        this.typeOptions = response.data
      })
      this.getDicts('sys_user_sex').then(response => {
        this.sexOptions = response.data
      })
      this.getDicts('app_s_post').then(response => {
        this.positionOptions = response.data
      })
    },
    methods: {
      point() {
        let that = this
        that.markers = []
        this.map.openPoint(function (data) {
          that.$refs.map.clearMarkerFeature('point')
          that.form.xy = data[0] + ',' + data[1]
          that.form.x = data[0]
          that.form.y = data[1]
          that.map.closePoint()
          that.markers.push({
            coordinates: data,
            id: that.form.id || '1',
            title: that.form.name || '标注',
            target: 'point',
            animate: true,
            stopEvent: true,
            iconUrl: '/image/map/' + (that.form.type || 'loc-red') + '.png'
          })
          that.$refs.map.showMarkerFeature(that.markers, false, 'point')
          that.$forceUpdate()
        })
      },
      showPoint(e) {
        if (e.x !== 0 && e.y !== 0 && e.x !== null && e.x !== null) {
          let that = this
          that.markers = []
          that.markers.push({
            coordinates: [e.x, e.y],
            id: e.id || '1',
            title: e.name || '标注',
            target: 'point',
            animate: true,
            stopEvent: true,
            iconUrl: '/image/map/' + (e.type || 'loc-red') + '.png'
          })
          that.$refs.map.showMarkerFeature(that.markers, false, 'point')
          that.$refs.map.map.getMap().getView().animate({
            center: [e.x, e.y],
            duration: 500
          })
          that.$forceUpdate()
        }
      },
      getappUserList() {
        this.loading = true
        listAppUser(this.queryappUserParams).then(response => {
          this.appUserList = response.rows
          this.totalappUser = response.total
          this.loading = false
        })
      },
      // 多选框选中数据
      handleSelectionChangeUser(selection) {
        if (selection.length > 1) {
          this.msgInfo('只能绑定一个巡护人员')
        }
        this.thisAppUser = selection
      },
      okappUser() {
        if (this.thisAppUser.length > 1) {
          this.msgInfo('只能绑定一个巡护人员')
        } else if (this.thisAppUser.length === 1) {
          this.form.patrolId = this.thisAppUser[0].userId
          this.form.patrolName = this.thisAppUser[0].name
          this.form.post = this.selectDictLabel(this.positionOptions, this.thisAppUser[0].position)
          this.form.deptName = this.thisAppUser[0].deptName
          this.form.mobile = this.thisAppUser[0].phonenumber
          this.openappUser = false
        } else {
          this.msgInfo('请选择巡护人员')
        }
      },
      cancelappUser() {
        this.openappUser = false
      },
      handleQueryappUser() {
        this.queryappUserParams.pageNum = 1
        this.getappUserList()
      },
      resetappUserQuery() {
        this.resetForm('queryappUserForm')
        this.handleQueryappUser()
      },
      selectPatrol() {
        this.openappUser = true
        this.handleQueryappUser()
      },
      /** 查询信息登记列表 */
      getList() {
        this.loading = true
        listDeployment(this.queryParams).then(response => {
          this.deploymentList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 类型字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.type)
      },
      // 性别字典翻译
      sexFormat(row, column) {
        return this.selectDictLabel(this.sexOptions, row.sex)
      },
      positionFormat(row, column) {
        return this.selectDictLabel(this.positionOptions, row.position)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          shipOffice: undefined,
          x: undefined,
          y: undefined,
          workStation: undefined,
          zoneArea: undefined,
          patrolId: undefined,
          patrolName: undefined,
          deptName: undefined,
          mobile: undefined,
          serviceId: undefined,
          serviceName: undefined,
          updateById: undefined,
          updateBy: undefined,
          updateTime: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        let that = this
        getDeployment(id).then(response => {
          that.form = response.data
          if (that.form.x !== null && response.data.y !== null) {
            that.form.xy = response.data.x + ',' + response.data.y
          }
          if (that.form.patrolId !== null) {
            getUser(that.form.patrolId).then(rs => {
              const user = rs.data
              if (user.dept !== null && user.dept !== undefined) {
                that.form.deptName = user.dept.deptName
              }
              that.form.post = that.selectDictLabel(that.positionOptions, user.position)
              that.form.mobile = user.phonenumber
              that.open = true
              that.$forceUpdate()
            })
          } else {
            that.open = true
            that.$forceUpdate()
          }
          this.title = '修改信息登记'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              editExt(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams1 = {...this.queryParams}
        queryParams1.pageSize = 9999
        queryParams1.pageNum = 1
        this.$confirm('是否确认导出所有信息登记数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return exportDeployment(queryParams1)
        }).then(response => {
          this.download(response.msg)
        })
      },
      handleOpenDialog() {
        this.$nextTick(() => {
          this.map = this.$refs.map.map
          this.showPoint(this.form)
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .dialog_con {
    .leftForm {
      padding: 20px;
      overflow: auto;
      height: calc(100vh - 108px);

      ::v-deep .el-table {
        height: 200px;
      }

      ::v-deep .el-table__body-wrapper {
        height: calc(100% - 44px);
        overflow-y: auto;
      }
    }

    ::v-deep .el-dialog__body {
      padding: 0;
    }

    ::v-deep .el-dialog__header {
      padding: 10px 20px 10px;
      background-color: #f2f2f2;

      .el-dialog__headerbtn {
        top: 12px;
      }
    }

    ::v-deep .el-dialog__footer {
      text-align: center;
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      padding: 15px;
      background: #f1f1f1;
    }

    .rightMap {
      height: calc(100vh - 108px);
      overflow-y: auto;
    }

  }
</style>
